<template>
  <div class="box">
    <div class="logo">
      <p>Welcome</p>
    </div>
    <div class="login-box">
      <div class="login-info">
        <div class="input-container">
          <input
            class="el-input_inner"
            v-model="username"
            placeholder="用户名"
          />
          <input class="el-input_inner" v-model="nickname" placeholder="昵称" />
          <input class="el-input_inner" v-model="phone" placeholder="手机" />
          <input class="el-input_inner" v-model="email" placeholder="邮箱" />
          <input class="el-input_inner" v-model="password" placeholder="密码" />
          <input
            class="el-input_inner"
            v-model="password_again"
            placeholder="确认密码"
          />
        </div>
        <div class="message-container">
          <a>清空</a>
          <router-link to="/"><a>点击登录</a></router-link>
        </div>
      </div>
      <button class="login-button" @click="register()">
        <span>注册</span>
      </button>
    </div>
  </div>
</template>

<script>
import { ref } from "@vue/reactivity";
import { getCurrentInstance } from "@vue/runtime-core";
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

export default {
  name: "sign-up",
  setup() {
    const instance = getCurrentInstance();
    const ctx = instance.appContext.config.globalProperties;

    let username = ref("");
    let nickname = ref("");
    let phone = ref("");
    let email = ref("");
    let password = ref("");
    let password_again = ref("");

    let router = useRouter();

    function register() {
      if (this.password !== this.password_again) {
        ElMessage('两次输入的密码不一致')
      }  else {
        ctx.$http
          .post("/user/register", {
            username: this.username,
            nickname: this.nickname,
            phone: this.phone,
            email: this.email,
            password: this.password,
          })
          .then((res) => {
            if(res.data.code === '201'){
              ElMessage('用户名已经被使用')
            }
            else{
              ElMessage('注册成功')
              router.push('/login')
            }
          })
          .catch((err) => {
            console.log(err);
            ElMessage('服务器错误')
          });
      }
    }

    return {
      username,
      nickname,
      phone,
      email,
      password,
      password_again,
      register,
    };
  },
};
</script>

<style scoped>
.box {
  background: linear-gradient(
      to bottom,
      rgba(255, 255, 255, 0),
      rgba(255, 255, 255, 0.7)
    ),
    url(../assets/blue.jpg), no-repeat;
  background-size: cover;
  height: 100%;
  width: 100%;
  position: absolute;
  top: 0;
  left: 0;
  overflow-y: auto;
}
.logo {
  height: 15%;
  width: 100%;
  padding-top: 20px;
  padding-bottom: 20px;
  color: white;
  font-family: Bahnschrift;
  font-size: 50px;
  text-align: center;
}
.login-box {
  display: flex;
  flex-direction: column;
  background: rgba(255, 255, 255, 0.7); /*设置半透明*/
  box-shadow: 1px 5px 15px #c0c0c0;
  margin: 10px auto;
  height: 60%;
  width: 85%;
  border-radius: 6px;
}
.login-info {
  display: flex;
  flex-direction: column;
  padding: 30px;
}

.el-input_inner {
  width: 220px;
  height: 10px;
  border-top-width: 0px;
  border-left-width: 0px;
  border-right-width: 0px;
  border-bottom-width: 1px;
}
.input-container {
  padding: 0;
}
.input-container input {
  border: 0;
  background: none;
  outline: 0;
  color: #003bb9;
  margin: 20px 0;
  display: block;
  width: 100%;
  padding: 5px 0;
  transition: 0.2s;
  border-bottom: 1px solid rgb(20, 111, 231);
}

.input-container input:hover {
  border-bottom-color: #87b5fc;
}

::-webkit-input-placeholder {
  color: rgb(121, 175, 238);
}

.message-container {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  height: 5px;
  font-size: 14px;
  transition: 0.2s;
  color: #87b5fc;
  cursor: pointer;
}

.message-container:hover {
  color: rgb(135, 180, 232);
}

.login-button {
  background-color: #0081ff;
  color: #ffffff;
  margin: 10px auto;
  height: 10%;
  width: 30%;
  border: none;
  border-radius: 50px;
}
.login-button:hover {
  background-color: rgb(135, 180, 232);
}
</style>